/*******************************************************************************
 * Copyright (c) 2010, 2017 西安秦晔信息科技有限公司
 * Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 *******************************************************************************/
package com.qinyeit.webapp.advice;

import com.qinyeit.serviceapp.exception.BusinessRunTimeException;
import com.qinyetech.springstage.core.http.ResponseStatusCode;
import com.qinyetech.springstage.core.web.dto.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.UnauthorizedException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * <p>ClassName: com.qinyeit.webapp.advice.GlobalExceptionHandlerAdvice
 * <p>Function: 全局异常处理
 * <p>date: 2018-12-22 17:41
 *
 * @author wuqing
 * @version 4.0
 * @since JDK 1.8
 */

@Slf4j
@RestControllerAdvice({
        "com.qinyeit.webapp.controller.management"
})
public class GlobalExceptionHandlerAdvice {
    /**
     * 未授权统一错误处理
     *
     * @param ex
     * @return
     */
    @ExceptionHandler(value = UnauthorizedException.class)
    public ResponseEntity<ResponseResult<String>> handleException(UnauthorizedException ex) {
        ResponseResult<String> response = new ResponseResult<>(false, "未授权禁止访问", "", ResponseResult.MessageLevel.error);
        response.setStatus(ResponseStatusCode.SC_FORBIDDEN);//403 未授权禁止访问
        log.error("403:未授权 {}",ex.getMessage());
        return new ResponseEntity<>(response, HttpStatus.FORBIDDEN);
    }

    /**
     * 业务异常处理
     * @param ex
     * @return
     */
    @ExceptionHandler(value = BusinessRunTimeException.class)
    public ResponseEntity<ResponseResult<String>> handleException(BusinessRunTimeException ex) {
        String message="错误编码:%d,%s";
        ResponseResult<String> response = new ResponseResult<>(false,
                String.format(message,ex.getErrorCode(),ex.getMessage()), "", ResponseResult.MessageLevel.error);
        response.setStatus(ResponseStatusCode.SC_SERVICE_UNAVAILABLE);//503 业务错误,无法完成请求
        log.error("业务异常,错误编号:"+ex.getErrorCode()+","+ex.getMessage(),ex);
        return new ResponseEntity<>(response, HttpStatus.SERVICE_UNAVAILABLE);
    }

    /**
     * 统一异常处理
     *
     * @param ex
     * @return
     */
    @ExceptionHandler(value = Exception.class)
    public ResponseEntity<ResponseResult<String>> handleException(Exception ex) {
        ResponseResult<String> response = new ResponseResult<String>();
        response.setSuccess(false);
        log.error(ex.getMessage(),ex);
        response.setMessage(ex.getMessage());
        response.setLevel(ResponseResult.MessageLevel.error);
        response.setStatus(ResponseStatusCode.SC_INTERNAL_SERVER_ERROR); //500 服务器内部错误
        return new ResponseEntity<>(response, HttpStatus.INTERNAL_SERVER_ERROR);
    }



}
